home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-1.iso / Files / Educ / Calc / MathPad 2.35.sit / XFuns / XFun kit / imageRGB.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-29  |  3.3 KB  |  140 lines  |  [TEXT/KAHL]

  1. /* XFun to allow rgb image array */
  2. #include "callback.h"
  3.  
  4. PixMapHandle DirectPix(long rows,long cols)    // create a 16-bit RGBDirect pixmap
  5. {
  6.     PixMapHandle pixh;
  7.     PixMapPtr pm;
  8.     Ptr pixels;
  9.     pixh = NewPixMap();
  10.     pixels = NewPtrClear(rows*cols*2);
  11.     if(!pixh || !pixels) return(NULL);
  12.     
  13.     pm = *pixh;        // don't need to lock. no calls
  14.     pm->pixelType = RGBDirect;
  15.     pm->pixelSize = 16;
  16.     pm->cmpCount = 3;
  17.     pm->cmpSize = 5;
  18.     
  19.     pm->bounds.top = 0;
  20.     pm->bounds.left = 0;
  21.     pm->bounds.bottom = cols;
  22.     pm->bounds.right = rows;
  23.  
  24.     pm->baseAddr = pixels;
  25.     pm->rowBytes = 0x8000 | rows*2;    // hi bit flags pixmap/bitmap
  26.     return(pixh);
  27. }
  28.  
  29. PicHandle MakePICT(PixMapHandle pixh)        // copy pixmap to PICT & dispose pixmap
  30. {
  31.    PixMapPtr srcpm,dstpm;
  32.    PicHandle thePic;
  33.    CGrafPtr picport,savport;
  34.       
  35.    /* The current grafport will be a text window which is not even a color port.
  36.     Maybe it doesn't matter since the pixmap is never drawn, but it seems
  37.     safer to make a new grafport for the pict.
  38.     This routine may run out of memory since it needs space for the pixmap
  39.     and the PICT at the same time */ 
  40.        
  41.    picport = (CGrafPtr)NewPtr(sizeof(CGrafPort));
  42.    OpenCPort(picport);
  43.    GetPort(&savport);
  44.    SetPort(picport);
  45.    HLock(pixh);
  46.    srcpm = *pixh;
  47.    thePic = OpenPicture(&srcpm->bounds);
  48.    CopyBits(srcpm,*(picport->portPixMap),&srcpm->bounds,&srcpm->bounds,ditherCopy,0);
  49.    HUnlock(pixh);
  50.    DisposePixMap(pixh);    // free up mem as soon as possible
  51.    ClosePicture();
  52.    SetPort(savport);
  53.    CloseCPort(picport);
  54.    DisposPtr(picport);
  55.    return(thePic);
  56. }
  57.  
  58. badparm(EXPR arr,funptr callback)
  59. {
  60.     ErrMsg(" imageRGB(?) array dim?",0,callback);
  61.     FreeExpr(arr,callback);
  62. }
  63.  
  64. BOOL imageRGB(extended *retval,funptr callback)
  65. {
  66.    PixMapHandle pixh;
  67.    EXPR arr;
  68.    extended num,*iptr,*jptr,*rgb;
  69.    long rows,cols,n,zero;
  70.    BOOL isarray;
  71.    short pak,comp,*pix,k;
  72.    long i,j;
  73.    
  74.    MakeParmExpr(0,&arr,callback);
  75.    
  76.    ProbeExpr(arr,&num,&isarray,&rows,callback);
  77.    if(!isarray || !rows)
  78.    {
  79.     badparm(arr,callback);
  80.     return(FALSE);
  81.    }
  82.    
  83.    AddIndex(&arr,&iptr,callback);
  84.    ProbeExpr(arr,&num,&isarray,&cols,callback);
  85.    if(!isarray || !cols)
  86.    {
  87.     badparm(arr,callback);
  88.     return(FALSE);
  89.    }
  90.  
  91.    pixh = DirectPix(rows,cols);
  92.    if(!pixh)
  93.    {
  94.     ErrMsg(" can't alloc pixmap",0,callback);
  95.     FreeExpr(arr,callback);
  96.     return(FALSE);
  97.    }
  98.    pix = (short *)((*pixh)->baseAddr);
  99.    
  100.    AddIndex(&arr,&jptr,callback);    /* arr[i,j] is {R,G,B}. Could use GetExprMatrix() to get
  101.                                           a row at a time but that could be a lot of space. */
  102.    for(i=0;i<rows;i++)
  103.    {
  104.     *jptr = 1;
  105.     for(j=cols-1;j>=0;j--)        // "sideways" image[x,y] convention
  106.     {
  107.      if(GetExprMatrix(arr,&rgb,&n,&zero,callback) && zero == 0 && n == 3)
  108.      {
  109.       pak = 0;
  110.       for(k=0; k<3; k++)
  111.       {
  112.        comp = rgb[k] * 31.0;
  113.        if(comp>31 || comp<0) ErrMsg(" RGB component must be 0 to 1",0,callback);
  114.        pak = (pak<<5) | comp;
  115.       }
  116.       DisposPtr(rgb);
  117.      }
  118.      else
  119.      {
  120.       DisposePixMap(pixh);
  121.       badparm(arr,callback);    // or some other error came first
  122.       return(FALSE);
  123.      }
  124.      pix[j*rows+i] = pak;
  125.      *jptr += 1.0;
  126.     }
  127.     *iptr += 1.0;
  128.    }
  129.    FreeExpr(arr,callback);
  130.    
  131.    SetPlotPICT(MakePICT(pixh),callback);
  132.    return(FALSE);
  133. }
  134.  
  135.  
  136. main(funptr callback)
  137. {
  138.     AddXfun("imageRGB","rgbarray",&imageRGB,NULL,callback);
  139. }
  140.